using System;
using System.Collections.Generic;
using System.Text;

namespace XDL.Framework.Configuration
{
    /// <summary>
    /// Helper functions to parse XML values
    /// </summary>
    class XmlHelper
    {
        /// <summary>
        /// Convert a float string. 
        /// </summary>
        /// <param name="f">float string</param>
        /// <returns>a <see cref="float"/> value.</returns>
        static public float ReadXmlFloat(string f)
        {
            return float.Parse(
                f,
                System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
        }

        /// <summary>
        /// Convert an array of coma (;) separated float values into a vector.
        /// </summary>
        /// <typeparam name="T">Template parameter. Must support a construtor with a T[] parameter.</typeparam>
        /// <param name="rawPosition">values</param>
        /// <returns>A new instance of thge </returns>
        static public T ReadXmlVector<T>(string values)
        {
            string[] strValues = values.Split(';');
            object[] floatValues = new object[strValues.Length];
            Type[] valueTypes = new Type[strValues.Length];
            for (int i = 0; i < floatValues.Length; i++)
            {
                floatValues[i] = ReadXmlFloat(strValues[i]);
                valueTypes[i] = typeof(float);
            }
            return (T)typeof(T).GetConstructor(valueTypes).Invoke(floatValues);
        }

        /// <summary>
        /// Read an array of coma (;) separated int value into a list
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        static public List<int> ReadXmlList(string values)
        {
            string[] strValues = values.Split(';');
            List<int> l = new List<int>(strValues.Length);
            for (int i = 0; i < strValues.Length; i++)
            {
                l.Add((int)ReadXmlFloat(strValues[i]));
            }
            return l;
        }
    }
}
